package com.lagou.service.impl;

import com.lagou.entity.Position;
import com.lagou.service.PositionService;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class PositionServiceImpl implements PositionService {

    @Autowired
    private SolrClient solrClient;

    public List<Position> queryPositions(String query){

        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("df", "positionName");
        solrQuery.setQuery(query);

        try {
            QueryResponse query1 = solrClient.query(solrQuery);
            List<Position> list = query1.getBeans(Position.class);
            if(list.size() < 5){
                // 查询的结果集为空则查询另外的条件补充五条
                SolrQuery solrQuery1 = new SolrQuery();
                solrQuery1.set("df", "positionAdvantage");
                solrQuery1.setStart(0);
                solrQuery1.setRows(5 - list.size());
                solrQuery1.setQuery("*美女多* OR *员工福利好*");
                QueryResponse query2 = solrClient.query(solrQuery1);
                List<Position> list1 = query2.getBeans(Position.class);
                list.addAll(list1);
                return list;
            }
            return list;
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
